home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1999 / MacHack 1999.toast / The Hacks / NetPokerForMacOSX_Server / HoldEmHigh / Messager.m < prev    next >
Encoding:
Text File  |  1999-06-26  |  2.7 KB  |  87 lines

  1. #import "poker.h"
  2. #import "IRCTask.h"
  3.  
  4. @implementation Messager
  5.  
  6. // shared instance is for Public
  7. //
  8. NSString *MessageReceivedNotification = @"MessageReceivedNotification";
  9.  
  10. + (id)sharedInstance {
  11.    static id shared = nil;
  12.  
  13.    if (!shared) {
  14.        shared = [[self allocWithZone:NULL] messagerWithRecipient:@"All Players"];
  15.    }
  16.    return shared;
  17. }
  18.  
  19. static NSMutableDictionary *dict = nil;
  20.  
  21. + instanceWithRecipient:(NSString *)receipt {
  22.     return [dict objectForKey:receipt];
  23. }
  24.  
  25. + (void)addInstance:(id)instance key:(NSString *)receipt {
  26.     if (!dict) dict = [[NSMutableDictionary allocWithZone:NULL] init];
  27.     [dict setObject:instance forKey:receipt];
  28. }
  29.  
  30.  
  31. #define END_RANGE NSMakeRange([[tv string]length],0)
  32. + (void)appendString:(NSString *)string toText:(NSTextView *)tv newLine:(BOOL)newLine;
  33. {
  34.         [tv replaceCharactersInRange:END_RANGE withString:string];
  35.         if (newLine)
  36.                 [tv replaceCharactersInRange:END_RANGE withString:@"\n"];
  37.         else
  38.                 [tv replaceCharactersInRange:END_RANGE withString:@" "];
  39.  
  40.         if ([[tv window] isVisible]) {
  41.                 [tv scrollRangeToVisible:END_RANGE];
  42.         }
  43. }
  44.  
  45. - (void)addMessage:(NSString *)message from:(NSString *)from {
  46.     [[self class] appendString:message toText:textView newLine:YES];
  47. }
  48.  
  49. - (void)gotMessage:(NSNotification *)notification
  50. {
  51.     NSString *fromWho = [notification object];
  52.     NSDictionary *dict =  [notification userInfo];
  53.     NSString *user = [[NSApp delegate] userName];
  54.     if ([fromWho isEqualToString:user] || [fromWho isEqualToString:recipient]) {
  55.     [self addMessage:[dict objectForKey:@"Message"] from:fromWho];
  56.     }
  57.  
  58. }
  59.  
  60. + (id)messagerWithRecipient:(NSString *)to {
  61.     if (self = [Messager instanceWithRecipient:to]) return self;
  62.     return [[self allocWithZone:NULL] messagerWithRecipient:to];
  63. }
  64.  
  65. - (id)messagerWithRecipient:(NSString *)to {
  66.    self = [self initWithWindowNibName:NSStringFromClass([self class])];
  67.    if (self) {
  68.        [self setWindowFrameAutosaveName:NSStringFromClass([self class])];
  69.        [[self window] setTitle:[NSString stringWithFormat:@"Messages to %@",to]];
  70.        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(gotMessage:) name:(NSString *)MessageReceivedNotification object:nil];
  71.        [Messager addInstance:self key:to];
  72.        recipient = [to copyWithZone:[self zone]];
  73.    }
  74.    return self;
  75. }
  76.  
  77. - (IBAction)sendMessageAction:(id)sender {
  78.    NSString *s = [sender stringValue];
  79.    if ([recipient isEqualToString:@"All Players"])
  80.        [[IRCTask sharedIRCTask] pushStringOverWire:[[NSString stringWithFormat:@"%@\n",s]cString]];
  81.     else
  82.         [[IRCTask sharedIRCTask] pushStringOverWire:[[NSString stringWithFormat:@"/msg %@ %@\n",recipient,s]cString]];
  83.  
  84. }
  85.  
  86. @end
  87.